﻿using System;
using RootMotion.FinalIK;
using UnityEngine;

namespace RootMotion.Demos
{
	// Token: 0x020000A3 RID: 163
	public class FPSAiming : MonoBehaviour
	{
		// Token: 0x0600056A RID: 1386 RVA: 0x00034F8C File Offset: 0x0003318C
		public void Start()
		{
			this.gunTargetDefaultLocalPosition = this.gunTarget.localPosition;
			this.gunTargetDefaultLocalRotation = this.gunTarget.localRotation;
			this.camDefaultLocalPosition = this.cam.transform.localPosition;
			this.cam.enabled = false;
			this.gunAim.enabled = false;
			this.ik.enabled = false;
		}

		// Token: 0x0600056B RID: 1387 RVA: 0x0000575B File Offset: 0x0000395B
		public void FixedUpdate()
		{
			this.updateFrame = true;
		}

		// Token: 0x0600056C RID: 1388 RVA: 0x00034FF8 File Offset: 0x000331F8
		public void LateUpdate()
		{
			if (!this.animatePhysics)
			{
				this.updateFrame = true;
			}
			if (!this.updateFrame)
			{
				return;
			}
			this.updateFrame = false;
			this.cam.transform.localPosition = this.camDefaultLocalPosition;
			this.camRelativeToGunTarget = this.gunTarget.InverseTransformPoint(this.cam.transform.position);
			this.cam.LateUpdate();
			this.RotateCharacter();
			this.ik.solver.leftHandEffector.positionWeight = ((this.aimWeight <= 0f || this.sightWeight <= 0f) ? 0f : (this.aimWeight * this.sightWeight));
			this.ik.solver.rightHandEffector.positionWeight = this.ik.solver.leftHandEffector.positionWeight;
			this.Aiming();
			this.LookDownTheSight();
		}

		// Token: 0x0600056D RID: 1389 RVA: 0x000350F4 File Offset: 0x000332F4
		public void Aiming()
		{
			if (this.aimWeight <= 0f)
			{
				return;
			}
			Quaternion rotation = this.cam.transform.rotation;
			this.gunAim.solver.IKPosition = this.cam.transform.position + this.cam.transform.forward * 10f;
			this.gunAim.solver.IKPositionWeight = this.aimWeight;
			this.gunAim.solver.Update();
			this.cam.transform.rotation = rotation;
		}

		// Token: 0x0600056E RID: 1390 RVA: 0x0003519C File Offset: 0x0003339C
		public void LookDownTheSight()
		{
			float num = this.aimWeight * this.sightWeight;
			if (num <= 0f)
			{
				return;
			}
			this.gunTarget.position = Vector3.Lerp(this.gun.position, this.gunTarget.parent.TransformPoint(this.gunTargetDefaultLocalPosition), num);
			this.gunTarget.rotation = Quaternion.Lerp(this.gun.rotation, this.gunTarget.parent.rotation * this.gunTargetDefaultLocalRotation, num);
			Vector3 position = this.gun.InverseTransformPoint(this.ik.solver.leftHandEffector.bone.position);
			Vector3 position2 = this.gun.InverseTransformPoint(this.ik.solver.rightHandEffector.bone.position);
			Quaternion rhs = Quaternion.Inverse(this.gun.rotation) * this.ik.solver.leftHandEffector.bone.rotation;
			Quaternion rhs2 = Quaternion.Inverse(this.gun.rotation) * this.ik.solver.rightHandEffector.bone.rotation;
			this.ik.solver.leftHandEffector.position = this.gunTarget.TransformPoint(position);
			this.ik.solver.rightHandEffector.position = this.gunTarget.TransformPoint(position2);
			this.ik.solver.headMapping.maintainRotationWeight = 1f;
			this.ik.solver.Update();
			this.ik.references.leftHand.rotation = this.gunTarget.rotation * rhs;
			this.ik.references.rightHand.rotation = this.gunTarget.rotation * rhs2;
			this.cam.transform.position = Vector3.Lerp(this.cam.transform.position, this.gun.transform.TransformPoint(this.camRelativeToGunTarget), num);
		}

		// Token: 0x0600056F RID: 1391 RVA: 0x000353D0 File Offset: 0x000335D0
		public void RotateCharacter()
		{
			if (this.maxAngle >= 180f)
			{
				return;
			}
			if (this.maxAngle <= 0f)
			{
				base.transform.rotation = Quaternion.LookRotation(new Vector3(this.cam.transform.forward.x, 0f, this.cam.transform.forward.z));
				return;
			}
			Vector3 vector = base.transform.InverseTransformDirection(this.cam.transform.forward);
			float num = Mathf.Atan2(vector.x, vector.z) * 57.29578f;
			if (Mathf.Abs(num) > Mathf.Abs(this.maxAngle))
			{
				float angle = num - this.maxAngle;
				if (num < 0f)
				{
					angle = num + this.maxAngle;
				}
				base.transform.rotation = Quaternion.AngleAxis(angle, base.transform.up) * base.transform.rotation;
			}
		}

		// Token: 0x0400044F RID: 1103
		[Range(0f, 1f)]
		public float aimWeight = 1f;

		// Token: 0x04000450 RID: 1104
		[Range(0f, 1f)]
		public float sightWeight = 1f;

		// Token: 0x04000451 RID: 1105
		[Range(0f, 180f)]
		public float maxAngle = 80f;

		// Token: 0x04000452 RID: 1106
		[SerializeField]
		public bool animatePhysics;

		// Token: 0x04000453 RID: 1107
		[SerializeField]
		public Transform gun;

		// Token: 0x04000454 RID: 1108
		[SerializeField]
		public Transform gunTarget;

		// Token: 0x04000455 RID: 1109
		[SerializeField]
		public FullBodyBipedIK ik;

		// Token: 0x04000456 RID: 1110
		[SerializeField]
		public AimIK gunAim;

		// Token: 0x04000457 RID: 1111
		[SerializeField]
		public CameraControllerFPS cam;

		// Token: 0x04000458 RID: 1112
		public Vector3 gunTargetDefaultLocalPosition;

		// Token: 0x04000459 RID: 1113
		public Quaternion gunTargetDefaultLocalRotation;

		// Token: 0x0400045A RID: 1114
		public Vector3 camDefaultLocalPosition;

		// Token: 0x0400045B RID: 1115
		public Vector3 camRelativeToGunTarget;

		// Token: 0x0400045C RID: 1116
		public bool updateFrame;
	}
}
